var models = require('../db');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');

var conn = mysql.createConnection(models.mysql);
conn.connect();
var jsonWrite = function(res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        });
    } else {
        res.json(
            ret
        );
    }
};
// 增加用户接口
router.post('/teacherRegister', (req, res) => {
    var sql = $sql.user.add;
    var params = req.body;
    var sql2 = " select * from teacher where userName = '" + params.username + "'";
    // console.log(params);
    conn.query(sql2, function(err, result) {
        if (err) {
            console.log('查询出错:' + err);
            return;
        };
        console.log("result", result)
        if (result.length == 0) {

            console.log(("无重名用户"))
            conn.query(sql, [params.username, params.password], function(err, result) {
                if (err) {
                    console.log('注册出错:' + err);
                    return;
                };
                res.send({
                    code: 200,
                    message: "注册成功"
                })
            })
        } else {
            console.log(("用户已存在"))
            res.send({
                code: 0,
                message: "用户已存在"
            })
        }
    })
});
router.post('/teacherLogin', (req, res) => {
    var params = req.body;
    var sql = " select * from teacher where userName = '" + params.username + "' and password = '" + params.password + "'  ";
    // console.log(params);
    conn.query(sql, [params.username, params.password], function(err, result) {
        if (err) throw err;
        console.log("result", result)
        if (result.length == 0) {
            console.log(("用户名或密码错误"))
            res.send({
                code: 0,
                message: "用户名或密码错误"
            })
        } else {
            console.log(("登录成功"))
            res.send({
                code: 200,
                message: "登录成功"
            })
        }
    })
});

router.post('/studentRegister', (req, res) => {
    var sql = $sql.stu.add;
    var params = req.body;
    var sql2 = " select * from student where userName = '" + params.username + "'";
    console.log(params);
    conn.query(sql2, function(err, result) {
        if (err) {
            console.log('查询出错:' + err);
            return;
        };
        console.log("result", result)
        if (result.length == 0) {

            console.log(("无重名用户"))
            conn.query(sql, [params.username, params.password], function(err, result) {
                if (err) {
                    console.log('注册出错:' + err);
                    return;
                };
                res.send({
                    code: 200,
                    message: "注册成功"
                })
            })
        } else {
            console.log(("用户已存在"))
            res.send({
                code: 0,
                message: "用户已存在"
            })
        }
    })
});



router.post('/studentLogin', (req, res) => {
    var params = req.body;
    var sql = " select * from student where userName = '" + params.username + "'  and password = '" + params.password + "' ";
    console.log(params);
    conn.query(sql, [params.username, params.password], function(err, result) {
        if (err) throw err;
        console.log("result", result)
        if (result.length == 0) {
            console.log(("用户名或密码错误"))
            res.send({
                code: 0,
                message: "用户名或密码错误"
            })
        } else {
            console.log(("登录成功"))
            res.send({
                code: 200,
                message: "登录成功"
            })
        }
    })
});


router.post('/LoginQuery', (req, res) => {
    var params = req.body;
    console.log(params);
    if (params.ididentity = 'teacher') {
        console.log('1');
        var sql = " select * from teacher where userName =? ";
    } else {
        console.log('2');
        var sql = " select * from student where userName =? ";
    }

    // conn.query(`select * from  teacher where userName='444'`, function(err, row) {
    conn.query(sql, [params.username], function(err, row) {
        if (err) {
            console.log(err)
        }
        console.log(typeof row)
        let data = JSON.stringify(row)

        res.end(data)

    })
});


router.post('/booksearch', (req, res) => {
    var params = req.body;
    console.log(params);
    // var sql = "select * from books where bookname like ?";
    var sql = "select bookid,img,bookname,author,publisher,time,introduction from books where bookname like ?";
    console.log('sql为' + sql);
    conn.query(sql, ["%" + params.bookname + "%"], function(err, row) {
        if (err) {
            console.log(err)
        }
        console.log(typeof row)
        let data = JSON.stringify(row)
        console.log(data);
        res.end(data)

    })
});


router.post('/bookinfo', (req, res) => {
    var params = req.body;
    console.log(params);
    var sql = " select * from books where bookid = '" + params.bookid + "'  ";
    conn.query(sql, [params.bookid], function(err, row) {
        if (err) {
            console.log(err)
        }
        console.log(typeof row)
        let data = JSON.stringify(row)

        res.end(data)

    })
});
router.post('/studenttask', (req, res) => {
    var params = req.body;
    console.log(params);
    var sql = "insert into studenttask(taskname,action,details,book,stime,etime) values (?,?,?,?,?,?)"
    conn.query(sql, [params.task.name, params.task.action, params.task.details, params.task.book, params.task.stime, params.task.etime], function(err, row) {
        if (err) {
            console.log(err)
        }

    })
});

router.post('/showbook', (req, res) => {

    var sql = " select * from books";
    conn.query(sql, function(err, row) {
        if (err) {
            console.log(err)
        }
        console.log(typeof row)
        let data = JSON.stringify(row)
        res.end(data)

    })
});
router.post('/showtask', (req, res) => {

    var sql = " select * from studenttask";
    conn.query(sql, function(err, row) {
        if (err) {
            console.log(err)
        }
        console.log(typeof row)
        let data = JSON.stringify(row)
        res.end(data)

    })
});


// router.get('/query', (req, res) => {
//     conn.query('select * from user', function(err, row) {
//         if (err) {
//             console.log(err)
//         }
//         console.log(typeof row)
//         let data = JSON.stringify(row)
//         res.end(data)
//     })
// });
// // 查询用户接口
//
// // 修改用户接口
// router.post('/updateUser', (req, res) => {
//   var params = req.body;
//   var sql = " update user set password = '"+ params.password + "' where username = '" + params.username + "'";
//   console.log(params);
//   conn.query(sql, [params.username], function(err, result) {
//     if (err) {
//       console.log(err);
//     }
//     if (result) {
//       console.log(res);
//       jsonWrite(res, result);
//     }
//   })
// });
//
// 删除用户接口
// router.post('/delUser', (req, res) => {
//     var params = req.body;
//     var sql = " delete from user where userName = '" + params.username + "'";
//     console.log(params);
//     conn.query(sql, [params.username], function(err, result) {
//         if (err) {
//             console.log(err);
//         }
//         if (result) {
//             console.log(res);
//             jsonWrite(res, result);
//         }
//     })
// });

module.exports = router;